package com.lyj.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LIUYANJIAO
 * Date: 2025-08-13
 * Time: 16:46
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    // 插入操作时，填充策略
    @Override
    public void insertFill(MetaObject metaObject) {
        // 填充 createBy，这里你可以根据实际业务获取当前用户 ID，比如从 ThreadLocal、SecurityContext 等获取
        this.strictInsertFill(metaObject, "createBy", Long.class, 1955553615381278721L);

        // 填充 createTime 为当前时间
        this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);

    }

    // 更新操作时，填充策略
    @Override
    public void updateFill(MetaObject metaObject) {
        // TODO 填充 updateBy，同样示例写死，实际替换成获取“更新人”ID逻辑
        this.strictUpdateFill(metaObject,"updateBy",Long.class,1955553615381278721L);
        // 填充 updateTime 为当前时间
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
    }
}
